package com.itchu.system.controller;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Song;
import com.itchu.system.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 这里的控制器全部都是后台系统的，需要认证才可以访问的
 */

@RestController
@RequestMapping("/song")
public class SongController {

    @Autowired
    private SongService songService;

    /**
     * ----------------------
     * 查找歌曲的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Song>> showAll(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page" , defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy") String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc
    ) {
        PageResult<Song> list = songService.showAll(key, page, rows,sortBy,desc);
        return ResponseEntity.ok(list);
    }

    /**
     * -------------------------
     * 添加歌曲操作
     * 添加的同时，不要忘记操作歌曲_分类中间表
     * @param song   歌曲对象
     * @param cids  分类ids
     * @return
     */
    @PostMapping("/saveSong")
    public ResponseEntity<Void> addSong(Song song,
                                        @RequestParam("cids") List<Long> cids){
        songService.addSong(song,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * ---------------------
     * 更新歌曲操作
     * @param song  歌曲对象
     * @param cids  分类ids
     * @return
     */
    @PutMapping("/saveSong")
    public ResponseEntity<Void> updateSong(Song song,
                                           @RequestParam("cids") List<Long> cids) {
        songService.updateSong(song,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * -----------------------
     * 删除歌曲操作
     * @param id  歌曲id
     * @return
     */
    @GetMapping("/delete/{id}")
    public ResponseEntity<Void> deleteSong(
            @PathVariable(value = "id") Long id
    ) {
        songService.deleteSong(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

//    /**
//     * =====================================
//     * 根据排行榜查询歌曲
//     * @param type   1,是飙升榜    2，人气榜      3，收藏榜
//     * @return
//     */
//    @GetMapping("/of/rank")
//    public ResponseEntity<List<Song>> findRankSongs(
//            @RequestParam(value = "type") Long type
//    ) {
//        List<Song> list = songService.findRankSongs(type);
//        return ResponseEntity.ok(list);
//    }

//    /**
//     * =================================
//     * 按名字做模糊搜索
//     * @param name
//     * @return
//     */
//    @GetMapping("search/Songs")
//    public ResponseEntity<List<Song>> searchSongsByName(@RequestParam(value = "name") String name) {
//        List<Song> list = songService.findSonsBySnameAndSiname(name);
//        return ResponseEntity.ok(list);
//    }
}
